//#include "StdAfx.h"
#include "TrainingCase.h"

CTrainingCase::CTrainingCase(void) {
	m_nCurrentSignalIdx = 0;
}

CTrainingCase::~CTrainingCase(void) {
}

CTrainingCase::CTrainingCase(EOBJTYPE tEnvRoundMe[ ALL_DIRECTION_COUNT], int myID) {
	memcpy(m_tEnvRoundMe, tEnvRoundMe, sizeof(EOBJTYPE) * ALL_DIRECTION_COUNT);
	//memset(m_nInputSignalSequence, 0, sizeof(m_nInputSignalSequence));
	m_nCurrentSignalIdx = 0;

	for (int j = 0; j < ALL_DIRECTION_COUNT; j++) {
		m_nInputSignalSequence[j].SetAsEOBJ(m_tEnvRoundMe[j]);
	}
	m_myID = myID;
}

DigitalSignal CTrainingCase::GetNextInputSignal() {
	DigitalSignal tmp;
	if (m_nCurrentSignalIdx < ALL_DIRECTION_COUNT)
		return m_nInputSignalSequence[m_nCurrentSignalIdx++];
	else
		return tmp;
}

RESULT_JUDGMENT CTrainingCase::CompareNervusOutputSignal(
		DigitalSignal nNervusOutputVaule) {
	RESULT_JUDGMENT ret;
	DIRECTION tDir = nNervusOutputVaule.ReadAsDir();

	if (tDir == DIR_NONE) //not move
			{
		int i;
		for (i = 0; i < ALL_DIRECTION_COUNT; i++)
			if (m_tEnvRoundMe[i] == EOBJTYPE_FOOD) {
				ret = RES_COMMON;
				goto _result;
			}
		for (i = 0; i < ALL_DIRECTION_COUNT; i++)
			if (m_tEnvRoundMe[i] != EOBJTYPE_CREATURE)
				break;

		if (i == ALL_DIRECTION_COUNT) {
			ret = RES_GOOD;
			goto _result;
		} else {
			ret = RES_COMMON;
			goto _result;
		}
	}

	if (m_tEnvRoundMe[tDir] == EOBJTYPE_FOOD) {
		ret = RES_GOOD;
		goto _result;
	}

	if (m_tEnvRoundMe[tDir] == EOBJTYPE_CREATURE) {
		ret = RES_BAD;
		goto _result;
	}

	if (m_tEnvRoundMe[tDir] == EOBJTYPE_WATER) {
		int i;
		for (i = 0; i < ALL_DIRECTION_COUNT; i++)
			if (m_tEnvRoundMe[i] == EOBJTYPE_FOOD) {
				ret = RES_COMMON;
				goto _result;
			}

		if (i == ALL_DIRECTION_COUNT) {
			ret = RES_GOOD;
			goto _result;
		}
	}

	_result:
#if 0
	/*if (ret == RES_GOOD)
		printf("GOOD:");
	else
		printf("BAD:");
	*/
	if (ret != RES_GOOD)
	{
		printf("env [");
		for (int i = 0; i < ALL_DIRECTION_COUNT; i++)
			printf(" %d ", m_tEnvRoundMe[i]);
		printf("] [id %d] dir:%d {%f,%f,%f},[%d]\n", m_myID, tDir,
				nNervusOutputVaule.GetValue(0).Angle(),
				nNervusOutputVaule.GetValue(1).Angle(),
				nNervusOutputVaule.GetValue(2).Angle(),
				tDir == DIR_NONE ? 0 : m_tEnvRoundMe[tDir]);
	}
#endif
	return ret;
}

